package com.tencent.ncnnyolox.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.jeremyliao.liveeventbus.core.LiveEvent;
import com.tencent.ncnnyolox.R;
import com.tencent.ncnnyolox.basemodule.base.BaseDialog;
import com.tencent.ncnnyolox.basemodule.utils.LiveEventKey;
import com.tencent.ncnnyolox.basemodule.view.CommonEditDialog;
import com.tencent.ncnnyolox.net.ApiViewModel;
import com.tencent.ncnnyolox.net.bean.ChatPhraseBean;
import com.tencent.ncnnyolox.net.bean.ChatPhraseUpdateDTO;
import com.tencent.ncnnyolox.net.bean.CommonWordBean;
import com.tencent.ncnnyolox.net.bean.PhraseLabelBean;
import com.yd.basemodule.base.bean.BaseResult;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : yh
 *     e-mail : xxx@xx
 *     time   : 2024/01/04
 *     desc   : 常用语item
 * </pre>
 */
public class CommonCYAdapter extends RecyclerView.Adapter<CommonCYAdapter.ViewHolder> {

    private LifecycleOwner lifecycleOwner;
    private Context context;
    private List<ChatPhraseBean> list = new ArrayList<>();

    public CommonCYAdapter(Context context, LifecycleOwner viewModel) {
        this.context = context;
        this.lifecycleOwner = viewModel;
    }

    public void setDate(List<ChatPhraseBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    private CommonCYAdapterClickItemListener listener;
    public interface CommonCYAdapterClickItemListener{
        void onClick(String content);
    }

    public void CommonCYAdapterSetOnClickListener(CommonCYAdapterClickItemListener cyAdapterClickItemListener){
        this.listener=cyAdapterClickItemListener;
    }

    @NonNull
    @Override
    public CommonCYAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.common_changyong_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CommonCYAdapter.ViewHolder holder, int position) {
        int pos=position;
        holder.tvCommonContent.setText(list.get(pos).getContent());
        holder.rlContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(list.get(pos).getContent());

            }
        });
        holder.llEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new CommonEditDialog.Builder(context).setTitle("编辑")
                        .setContent(list.get(pos).getContent())
                        .setListener(new CommonEditDialog.Builder.OnListener() {
                            @Override
                            public void onCancel(@Nullable BaseDialog dialog) {
                                dialog.dismiss();
                            }

                            @Override
                            public void onConfirm(@Nullable BaseDialog dialog, @NonNull String text, PhraseLabelBean phraseLabelBean) {
                                ApiViewModel viewModel=new ApiViewModel();
                                ChatPhraseBean commonWordBean = list.get(pos);
                                ChatPhraseUpdateDTO bean=new ChatPhraseUpdateDTO(commonWordBean.getId(), commonWordBean.getMemberId(), text, commonWordBean.getLabelId(), commonWordBean.getDelFlag());
                                viewModel.updateCommonWord(bean).observe(lifecycleOwner, new Observer<BaseResult<String>>() {
                                    @Override
                                    public void onChanged(BaseResult<String> stringBaseResult) {
                                        if (stringBaseResult.code()==200){
                                            ToastUtils.showShort("添加成功");
                                            holder.tvCommonContent.setText(text);
                                            LiveEventBus.get(LiveEventKey.COMMON_EDIT).post(phraseLabelBean);
                                            dialog.dismiss();
                                        }else {
                                            ToastUtils.showShort("添加失败");
                                            LogUtils.e("CommonAdapter-updateCommonWord"+stringBaseResult.getMsg());
                                        }
                                    }
                                });
                            }
                        }).show();
            }
        });

        holder.llDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ApiViewModel viewModel=new ApiViewModel();
                viewModel.deleteCommonData(list.get(pos).getId()).observe(lifecycleOwner, new Observer<BaseResult<String>>() {
                    @Override
                    public void onChanged(BaseResult<String> stringBaseResult) {
                        if (stringBaseResult.code()==200){
                            ToastUtils.showShort("删除成功");

                            LiveEventBus.get(LiveEventKey.COMMON_EDIT).post("delete");
                        }else {
                            ToastUtils.showShort("删除失败");
                            LogUtils.e("CommonAdapter"+stringBaseResult.getMsg());
                        }
                    }
                });

            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout llDelete;
        LinearLayout llEdit;
        TextView tvCommonContent;
        RelativeLayout rlContent;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCommonContent = itemView.findViewById(R.id.tv_common_content);
            llEdit = itemView.findViewById(R.id.ll_edit);
            llDelete = itemView.findViewById(R.id.ll_delete);
            rlContent=itemView.findViewById(R.id.rl_content);
        }
    }
}
